package cn.huugh;

import cn.huugh.dao.impl.UserDao;
import com.google.gson.Gson;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

public class competition extends HttpServlet {
    private ApplicationContext context;
    private UserDao a;
    public competition() {
        context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
         a = (UserDao)context.getBean("UserDao");
         a.connectDataBase();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Gson gson = new Gson();
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=utf-8");
        PrintWriter out = resp.getWriter();
        switch (req.getParameter("type")){
            case "0":
                if (!req.getParameter("value").equals("")){
                    out.print(gson.toJson(a.getTable(req.getParameter("value"))));
                }
                break;
            case "1"://输入推荐
                if (!req.getParameter("value").equals("")) {//优化500错误
                    out.print(gson.toJson(a.buildTips(a.getTips(req.getParameter("value")))));
                }
                break;
            default:
                resp.sendError(404);
        }
    }


    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        System.out.print(((UserDao)context.getBean("UserDao")).getTable("数学").toString());

    }
}
